import time
import datetime


class TimeUtil:
    def __init__(self):
        pass

    def get_time_stamp_datetime(self, time_str: str) -> int:
        # time_format = time.strptime(time_str, "%Y-%m-%d")  # 转为时间戳
        time_format = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")  # 转为时间戳
        return int(time.mktime(time_format))

    def get_time_stamp_date(self, time_str: str) -> int:
        time_format = time.strptime(time_str, "%Y-%m-%d")  # 转为时间戳
        # time_format = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")  # 转为时间戳
        return int(time.mktime(time_format))

    def utc_2_pk(self, utctime_str: str) -> datetime:
        '''UTC时间字符串转化为北京时间的datetime对象
        :参数 utctime_str:UTC时间字符串，格式为yyyy-m-d H
        '''
        # 构造出没有时区的datetime对象
        naive_time = datetime.datetime.strptime(utctime_str, '%Y-%m-%d %H:%M:%S')
        # 取出上述对象的年月日小时构造一个时区为utc的datetime对象
        utctime = datetime.datetime(naive_time.year, naive_time.month, naive_time.day, naive_time.hour,
                                    tzinfo=datetime.timezone.utc)
        # 把时区为utc的对象转化为时区为Asia/Shanghai的datetime对象
        beijing = datetime.timezone(datetime.timedelta(hours=8))
        pktime = utctime.astimezone(beijing)
        return pktime

    def pk_2_utc(self, pktime_str: str) -> datetime:
        '''北京时间字符串转化为UTC时间的datetime对象
        :参数 pktime_str:北京时间字符串，格式为yyyy-m-d H
        '''
        # 构造出没有时区的datetime对象
        naive_time = datetime.datetime.strptime(pktime_str, '%Y-%m-%d %H:%M:%S')
        # 将上述对象转化为时区为Asia/Shanghai的datetime对象
        beijing = datetime.timezone(datetime.timedelta(hours=8))
        pktime = naive_time.astimezone(beijing)
        # 将时区为上海的datetime对象转化为时区为utc的时间对象
        utctime = pktime.astimezone(datetime.timezone.utc)
        return utctime

    def local_2_utc_str(self, local_dtm):
        # 本地时间转 UTC 时间（ -8:00 ）
        return datetime.datetime.utcfromtimestamp(local_dtm.timestamp()).strftime('%Y-%m-%d %H:%M:%S')

    def get_date(self, date_str: str) -> datetime:
        return datetime.datetime.fromisoformat(date_str)

    def get_cur_date_str(self) -> str:
        return datetime.datetime.utcnow().strftime("%Y-%m-%d")

    def get_next_date_str(self) -> str:
        next_date = datetime.datetime.utcnow().date() + datetime.timedelta(days=1)
        return next_date.__format__('%Y-%m-%d')

    def get_cur_date_date(self) -> datetime:
        cur_date_str = self.get_cur_date_str()
        return self.get_date(cur_date_str)

    def get_next_date_date(self) -> datetime:
        next_date_str = self.get_next_date_str()
        print(next_date_str)
        print(type(next_date_str))
        return self.get_date(next_date_str)

    def get_datetime(self, iso_time: str) -> datetime:
        return datetime.datetime.sprptime(iso_time, '%Y-%m-%dT%H:%M:%S%z')
